#include "MarsPrecompile.h"

#include "MarsFbxMgr.h"
#include "MarsPipelineMgr.h"

using namespace Pipeline;

FbxMgr* FbxMgr::ms_pFbxMgr = 0;

void FbxMgr::Destroy()
{
	SAFE_DELETE( ms_pFbxMgr );
}

FbxMgr::FbxMgr()
: m_pFbxManager( NULL )
, m_pFbxScene( NULL )
{

}

FbxMgr::~FbxMgr()
{
	Shutdown();
}

bool FbxMgr::Initialize()
{
	//The first thing to do is to create the FBX Manager which is the object allocator for almost all the classes in the SDK
	m_pFbxManager = FbxManager::Create();
	if ( !m_pFbxManager )
	{
		FBXSDK_printf("Error: Unable to create FBX Manager!\n");
		return false;
	}
	else
	{
		FBXSDK_printf( "Autodesk FBX SDK version %s\n", m_pFbxManager->GetVersion() );
	}

	//Create an IOSettings object. This object holds all import/export settings.
	FbxIOSettings* ios = FbxIOSettings::Create( m_pFbxManager, IOSROOT );
	m_pFbxManager->SetIOSettings( ios );

	//Create an FBX scene. This object holds most objects imported/exported from/to files.
	m_pFbxScene = FbxScene::Create( m_pFbxManager, "Mars pipeline scene" );
	if ( !m_pFbxScene )
	{
		FBXSDK_printf("Error: Unable to create FBX scene!\n");
		Shutdown();
		return false;
	}

	return true;
}

void FbxMgr::PostInit()
{
	PipelineMgr::Instance().RegisterMeshImportMgr( this );
}

void FbxMgr::Shutdown()
{
	PipelineMgr::Instance().UnRegisterMeshImportMgr( GetMeshImportType() );

	if ( m_pFbxManager )
	{
		m_pFbxManager->Destroy();
		m_pFbxManager = NULL;
	}
}